package com.itheima.test02;

// 选择器排序代码测试

import java.util.Comparator;
import java.util.TreeSet;

public class Test02 {
    public static void main(String[] args) {
        Student student1 = new Student("张三", 23);
        Student student2 = new Student("王五", 28);
        Student student3 = new Student("王霸", 28);
        Student student4 = new Student("赵六", 12);
        Student student5 = new Student("赵六", 12);
        Student student6 = new Student("李四", 46);
        /*//用匿名内部类方法实现
        TreeSet<Student> treeSet = new TreeSet<>(new Comparator<Student>() {
            @Override
            public int compare(Student o1, Student o2) {
                int result = o1.getAge()-o2.getAge();
                result = result == 0 ? o1.getName().compareTo(o2.getName()) : result;
                return result;
            }
        });*/

        //用Lambda表达式实现
        TreeSet<Student> treeSet = new TreeSet<>((o1, o2) -> {
            int result = o1.getAge() - o2.getAge();
            result = result == 0 ? o1.getName().compareTo(o2.getName()) : result;
            return result;
        });
        treeSet.add(student1);
        treeSet.add(student2);
        treeSet.add(student3);
        treeSet.add(student4);
        treeSet.add(student5);
        treeSet.add(student6);

        System.out.println(treeSet);
    }
}
